package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
import cn.com.ghy.android.util.XMLStatic;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:客户信息解析器
 * @author wuz  @date 2011-5-12 10:07:22
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CustomerInfoBeanParser {
	public static String createXML(String customerNum, String customerName,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        xmlSerializer.startTag("", XMLStatic.PARTNER);
        xmlSerializer.text(customerNum);
		xmlSerializer.endTag("", XMLStatic.PARTNER);
		xmlSerializer.startTag("", XMLStatic.NAME_ORG1);
        xmlSerializer.text(customerName);
		xmlSerializer.endTag("", XMLStatic.NAME_ORG1);
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
	
	public static List<CustomerInfoBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        CustomerInfoBean customerInfo = null;
        List<CustomerInfoBean> list = null;
        LoginInfoBean loginInfo = new LoginInfoBean();
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<CustomerInfoBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table".equals(parser.getName())) {
                	customerInfo = new CustomerInfoBean();
                	
                } else if (null != customerInfo) {
                    if ("PARTNER".equals(parser.getName())) {   
                    	customerInfo.setPartner(parser.nextText());   
                    } else if("PERNR".equals(parser.getName())) {
                    	loginInfo.setPernr(parser.nextText());
                    } else if("SAOF".equals(parser.getName())) {
                    	loginInfo.setSaof(parser.nextText());
                    } else if("SAOFNAME".equals(parser.getName())) {
                    	loginInfo.setSaofname(parser.nextText());
                    } else if ("NAME_ORG1".equals(parser.getName())) {   
                    	customerInfo.setName_org1(parser.nextText());   
                    } else if ("CUST_TYPE".equals(parser.getName())) {   
                    	customerInfo.setCust_type(parser.nextText());   
                    } else if ("YTNAME".equals(parser.getName())) {   
                    	customerInfo.setYtname(parser.nextText());   
                    } else if ("DENGJI".equals(parser.getName())) {   
                    	customerInfo.setDengji(parser.nextText());   
                    } else if ("CK1NAME".equals(parser.getName())) {   
                    	customerInfo.setCk1name(parser.nextText());   
                    } else if ("CK2NAME".equals(parser.getName())) {   
                    	customerInfo.setCk2name(parser.nextText());   
                    } else if ("CYNAME".equals(parser.getName())) {   
                    	customerInfo.setCyname(parser.nextText());   
                    } else if ("QD".equals(parser.getName())) {   
                    	customerInfo.setQd(parser.nextText());   
                    } else if ("LXR".equals(parser.getName())) {   
                    	customerInfo.setLxr(parser.nextText());   
                    } else if ("LXDH".equals(parser.getName())) {   
                    	customerInfo.setLxdh(parser.nextText());   
                    } else if ("STREET".equals(parser.getName())) {   
                    	customerInfo.setStreet(parser.nextText());   
                    } else if ("MDMJ".equals(parser.getName())) {   
                    	customerInfo.setMdmj(parser.nextText());   
                    } else if ("CAPACITY".equals(parser.getName())) {   
                    	customerInfo.setCapacity(parser.nextText());   
                    } else if ("REMARK".equals(parser.getName())) {   
                    	customerInfo.setRemark(parser.nextText());   
                    } else if("PROVNAME".equals(parser.getName())) {
                    	customerInfo.setProvname(parser.nextText());
                    } else if("CITYNAME".equals(parser.getName())) { 
                    	customerInfo.setCityname(parser.nextText());
                    } else if("ZONENAME".equals(parser.getName())) { 
                    	customerInfo.setZonename(parser.nextText());
                    } else if("JDB".equals(parser.getName())) { 
                    	customerInfo.setJdb(parser.nextText());
                    } else if("JXSID".equals(parser.getName())) { 
                    	customerInfo.setJxsid(parser.nextText());
                    } else if("JXSNAME".equals(parser.getName())) { 
                    	customerInfo.setJxsname(parser.nextText());
                    } else if("FXSID".equals(parser.getName())) { 
                    	customerInfo.setFxsid(parser.nextText());
                    } else if("FXSNAME".equals(parser.getName())) { 
                    	customerInfo.setFxsname(parser.nextText());
                    } else if("QDNAME".equals(parser.getName())) { 
                    	customerInfo.setQdname(parser.nextText());
                    } else if("ISVISIT".equals(parser.getName())) { 
                    	customerInfo.setIsvisit(parser.nextText());
                    } else if("ISLEAVE".equals(parser.getName())) { 
                    	customerInfo.setIsleave(parser.nextText());
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table".equals(parser.getName())) {
                    if (null != customerInfo && null != list) {
                    	customerInfo.setLoginInfo(loginInfo);
                    	list.add(customerInfo);   
                    	customerInfo = null;   
                    }   
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
}
